package com.gzy.cs5.controller;

import com.gzy.cs5.Repository.InventoryRepository;
import com.gzy.cs5.Repository.MemoryInventoryRepository;
import com.gzy.cs5.exception.BusinessException;
import com.gzy.cs5.model.InventoryItem;
import com.gzy.cs5.service.InventoryService;
import com.gzy.cs5.service.SimpleInventoryService;

import java.util.List;

public class InventoryController {
    private InventoryService inventoryService;

    public InventoryController() {
        InventoryRepository inventoryRepository = new MemoryInventoryRepository();
        this.inventoryService = new SimpleInventoryService(inventoryRepository);
    }

    public void addItem(InventoryItem item) {
        try {
            inventoryService.addItem(item);
            System.out.println("库存项添加成功: " + item);
        } catch (BusinessException e) {
            System.out.println("库存项添加失败: " + e.getMessage());
        }
    }

    public void removeItem(String itemId) {
        if (itemId == null) {
            System.out.println("库存项ID未找到，无法移除库存项");
            return;
        }

        try {
            inventoryService.removeItem(itemId);
            System.out.println("库存项移除成功: " + itemId);
        } catch (BusinessException e) {
            System.out.println("库存项移除失败: " + e.getMessage());
        }
    }

    public void getAllItems() {
        try {
            List<InventoryItem> items = inventoryService.getAllItems();
            System.out.println("所有库存项: " + items);
        } catch (Exception e) {
            System.out.println("获取库存项失败: " + e.getMessage());
        }
    }

    public void getItemById(String itemId) {
        if (itemId == null) {
            System.out.println("库存项ID未找到，无法获取库存项详情");
            return;
        }

        try {
            InventoryItem item = inventoryService.getItemById(itemId);
            if (item != null) {
                System.out.println("库存项详情: " + item);
            } else {
                System.out.println("库存项不存在: " + itemId);
            }
        } catch (Exception e) {
            System.out.println("获取库存项详情失败: " + e.getMessage());
        }
    }

    public void updateItemQuantity(String itemId, int quantity) {
        if (itemId == null) {
            System.out.println("库存项ID未找到，无法更新库存项数量");
            return;
        }

        try {
            inventoryService.updateItemQuantity(itemId, quantity);
            System.out.println("库存项数量更新成功: " + itemId);
        } catch (BusinessException e) {
            System.out.println("库存项数量更新失败: " + e.getMessage());
        }
    }
}